//
//  HotHeaderView.m
//  YLProject
//
//  Created by YuanGu on 2018/1/11.
//  Copyright © 2018年 WY. All rights reserved.
//

#import "HotHeaderView.h"
#import "UIButton+ImageTitleSpace.h"

@implementation HotHeaderView{
    UIButton *_selectButton;
    NSMutableArray *_titles;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setUpUI];
    }
    return self;
}

#pragma mark - UI

- (void)setUpUI{
    
    NSArray *array = @[@"全部" ,@"上海市" ,@"健身教练" ,@"美食" ,
                       @"白领" ,@"舞蹈" ,@"健身" ,@"英语" ,
                       @"化妆" ,@"旅游" ,@"唱歌" ,@"更多"];
    
    _titles = [array mutableCopy];
    
    CGFloat width = (self.width-NormalWidth*5)/4;
    CGFloat height = NormalWidth*2.5;
    
    for (int i=0; i<12; i++) {
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        
        [button setTitle:_titles[i] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
        [button setFrame:CGRectMake(NormalWidth + (width+NormalWidth)*(i%4),
                                    NormalWidth + (height+NormalWidth)*(i/4),
                                    width,
                                    height)];
        button.layer.cornerRadius = height/2;
        button.tag = 150 + i;
        button.layer.borderWidth = 0.3;
        button.layer.borderColor = [[UIColor lightGrayColor] CGColor];
        button.titleLabel.font = [UIFont systemFontOfSize:(iPhone5S||iPhone4s) ? 12 : 13];
        
        if (i == 0) {
            button.selected = YES;
            _selectButton = button;
        }else if (i == 11){
            button.selected = NO;
            [button setImage:[UIImage imageNamed:@"排行_highlight"] forState:UIControlStateNormal];
            [button layoutWithEdgeInsetsStyle:ButtonEdgeInsetsStyleLeft
                              imageTitleSpace:2];
        }
        [button addTarget:self action:@selector(buttonClickAction:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:button];
    }
}



#pragma mark - button touch

- (void)buttonClickAction:(UIButton *)button{
    if (button.selected) {
        return;
    }
    if (self.delegate) {
        if (button.tag == 161) {
            
            if (self.delegate && [self.delegate respondsToSelector:@selector(hotHeaderViewClickMore:)]) {
                [self.delegate hotHeaderViewClickMore:button.tag - 150];
            }
        }else{
            
            if (self.delegate && [self.delegate respondsToSelector:@selector(hotHeaderViewClickTitles:index:)]) {
                [self.delegate hotHeaderViewClickTitles:button.titleLabel.text index:button.tag - 15];
            }
        }
    }
    
    if (button.tag != 161) {
        [self buttonBackgroundAction:button];
    }
}

- (void)buttonBackgroundAction:(UIButton *)button{
    if (_selectButton && _selectButton != button) {
        _selectButton.selected = NO;
        _selectButton.layer.borderColor = [[UIColor lightGrayColor] CGColor];
        _selectButton.backgroundColor = [UIColor whiteColor];
    }
    
    button.selected = YES;
    button.backgroundColor = kHEXCOLOR(@"f4f5f7");
    button.layer.borderColor = [[UIColor clearColor] CGColor];

    _selectButton = button;
}
/*这里处理外部修改第二个按钮*/
-(void)setButton:(NSString *)title{
    UIButton *button = (UIButton *)[self viewWithTag:151];
    [button setTitle:title forState:UIControlStateNormal];
    if (button.selected) {
        button.selected = NO;
    }
    [self buttonClickAction:button];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
